package com.webapps.tvprogram.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.webapps.tvprogram.R;
import com.webapps.tvprogram.data.local.Program;
import com.webapps.tvprogram.utils.GlobalInfo;

public class ProgramsListSelect extends Activity {

    private List<Map<String, Object>> list;
    private SimpleAdapter adapter;
    private static final String ICON = "icon";
    private static final String NAME = "name";
    private static final String[] PARAM = {ICON, NAME};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = new ArrayList<Map<String, Object>>();
        setContentView(R.layout.program_list_view_select);
        final List<Program> progsList = MainActivity.getInstance().getProgramsData();
        addItem(list, BitmapFactory.decodeResource(getResources(), R.drawable.icon), getResources()
                .getString(R.string.all_programs_on_list_title));
        for (Iterator<Program> iterator = progsList.iterator(); iterator.hasNext();) {
            Program p = iterator.next();
            addItem(list, p.getIcon(), p.getName());
        }
        adapter = new SimpleAdapter(this, list, R.layout.list_icon_text_elem, PARAM, new int[]{R.id.bimage, R.id.btitle});
        adapter.setViewBinder(new MyViewBinder());
        ListView lv = (ListView) findViewById(R.id.programs_list_select);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView parent, View v, int position, long id) {
                if (position == 0) {
                    Log.v("AAA", "click item nr " + position + ", means: ALL");
                    Intent i = new Intent(ProgramsListSelect.this, AllProgramsListWithIconActivity.class);
                    startActivity(i);
                } else {
                    Log.v("AAA", "click item nr " + position + ", means: " + progsList.get(position - 1).getName());
                    String progName = progsList.get(position - 1).getName();
                    Intent i = new Intent(ProgramsListSelect.this, SingleProgramList.class);
                    i.putExtra(GlobalInfo.BUNDLE_PROGRAM_NAME, progName);
                    startActivity(i);
                }
            }
        });
    }

    private void addItem(List<Map<String, Object>> data, Bitmap bitmap, String name) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(ICON, bitmap);
        map.put(NAME, name);
        data.add(map);
    }
}
